/*
' DotNetNuke?- http://www.dotnetnuke.com
' Copyright (c) 2002-2006
' by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
 */

using System;
using System.Data;
using System.Data.SqlClient;

using Microsoft.ApplicationBlocks.Data;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Framework.Providers;

namespace M2Land.Modules.FlashImageRotator
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The SqlDataProvider class is a SQL Server implementation of the abstract DataProvider
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    /// </history>
    /// -----------------------------------------------------------------------------
    public class SqlDataProvider : DataProvider
    {

        #region Private Members

        private const string ProviderType = "data";
        private const string ModuleQualifier = "M2Land_";

        private ProviderConfiguration _providerConfiguration = ProviderConfiguration.GetProviderConfiguration(ProviderType);
        private string _connectionString;
        private string _providerPath;
        private string _objectQualifier;
        private string _databaseOwner;

        #endregion

        #region Constructors

        /// <summary>
        /// Constructs new SqlDataProvider instance
        /// </summary>
        public SqlDataProvider()
        {
            //Read the configuration specific information for this provider
            Provider objProvider = (Provider)_providerConfiguration.Providers[_providerConfiguration.DefaultProvider];

            //Read the attributes for this provider
            //Get Connection string from web.config
            _connectionString = Config.GetConnectionString();

            if (_connectionString.Length == 0)
            {
                //Use connection string specified in provider
                _connectionString = objProvider.Attributes["connectionString"];
            }

            _providerPath = objProvider.Attributes["providerPath"];

            _objectQualifier = objProvider.Attributes["objectQualifier"];

            if ((_objectQualifier != "") && (_objectQualifier.EndsWith("_") == false))
            {
                _objectQualifier += "_";
            }

            _databaseOwner = objProvider.Attributes["databaseOwner"];
            if ((_databaseOwner != "") && (_databaseOwner.EndsWith(".") == false))
            {
                _databaseOwner += ".";
            }
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets and sets the connection string
        /// </summary>
        public string ConnectionString
        {
            get { return _connectionString; }
        }

        /// <summary>
        /// Gets and sets the Provider path
        /// </summary>
        public string ProviderPath
        {
            get { return _providerPath; }
        }

        /// <summary>
        /// Gets and sets the Object qualifier
        /// </summary>
        public string ObjectQualifier
        {
            get { return _objectQualifier; }
        }

        /// <summary>
        /// Gets and sets the database ownere
        /// </summary>
        public string DatabaseOwner
        {
            get { return _databaseOwner; }
        }

        #endregion

        #region Private Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the fully qualified name of the stored procedure
        /// </summary>
        /// <param name="name">The name of the stored procedure</param>
        /// <returns>The fully qualified name</returns>
        /// -----------------------------------------------------------------------------
        private string GetFullyQualifiedName(string name)
        {
            return DatabaseOwner + ObjectQualifier + ModuleQualifier + name;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the value for the field or DbNull if field has "null" value
        /// </summary>
        /// <param name="Field">The field to evaluate</param>
        /// <returns></returns>
        /// -----------------------------------------------------------------------------
        private Object GetNull(Object Field)
        {
            return Null.GetNull(Field, DBNull.Value);
        }

        #endregion

        #region Public Methods

        public override void AddFlashImageRotator(int ModuleId,string Source, int Height,int Width,string File,string Image,
            string ID,bool SearchBar,string BackColor,string FrontColor,string LightColor,string ScreenColor,
            string Logo, string OverStrech, bool ShowEq, bool ShowIcons, string Transtion, bool ShowNavigation, bool ShowStop,
            bool ShowDigits,bool ShowDownload,bool UseFullScreen,bool AutoScroll,int DisplayHeight,int DisplayWidth,
            bool ThumbsInPlaylist,string Audio,bool AutoStart,int BufferLength,string Captions,string FallBack,
            string Repeat, int RotateTime, bool Shuffle, bool Smoothing, int Volume, string CallBack, bool EnableJS,
            string JavascriptID,string Link,bool LinkFromDisplay,string LinkTarget,string Recommendation,string SearchLink,
            string StreamScript,string Type)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("FlashImageRotatorAdd"), ModuleId,Source,Height,Width,File,Image,
            ID,SearchBar,BackColor,FrontColor,LightColor,ScreenColor,
            Logo,OverStrech,ShowEq,ShowIcons,Transtion,ShowNavigation,ShowStop,
            ShowDigits,ShowDownload,UseFullScreen,AutoScroll,DisplayHeight,DisplayWidth,
            ThumbsInPlaylist,Audio,AutoStart,BufferLength,Captions,FallBack,
            Repeat,RotateTime,Shuffle,Smoothing,Volume,CallBack,EnableJS,
            JavascriptID,Link,LinkFromDisplay,LinkTarget,Recommendation,SearchLink,
            StreamScript,Type);
        }

        public override void DeleteFlashImageRotator(int ModuleId)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("FlashImageRotatorDelete"), ModuleId);
        }

        public override IDataReader GetFlashImageRotator(int ModuleId)
        {
            return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, GetFullyQualifiedName("FlashImageRotatorGet"), ModuleId);
        }



        public override void UpdateFlashImageRotator(int ModuleId,string Source, int Height,int Width,string File,string Image,
            string ID,bool SearchBar,string BackColor,string FrontColor,string LightColor,string ScreenColor,
            string Logo, string OverStrech, bool ShowEq, bool ShowIcons, string Transtion, bool ShowNavigation, bool ShowStop,
            bool ShowDigits,bool ShowDownload,bool UseFullScreen,bool AutoScroll,int DisplayHeight,int DisplayWidth,
            bool ThumbsInPlaylist,string Audio,bool AutoStart,int BufferLength,string Captions,string FallBack,
            string Repeat, int RotateTime, bool Shuffle, bool Smoothing, int Volume, string CallBack, bool EnableJS,
            string JavascriptID,string Link,bool LinkFromDisplay,string LinkTarget,string Recommendation,string SearchLink,
            string StreamScript,string Type)
        {
            SqlHelper.ExecuteNonQuery(ConnectionString, GetFullyQualifiedName("FlashImageRotatorUpdate"), ModuleId,Source, Height, Width, File, Image,
            ID, SearchBar, BackColor, FrontColor, LightColor, ScreenColor,
            Logo, OverStrech, ShowEq, ShowIcons, Transtion, ShowNavigation, ShowStop,
            ShowDigits, ShowDownload, UseFullScreen, AutoScroll, DisplayHeight, DisplayWidth,
            ThumbsInPlaylist, Audio, AutoStart, BufferLength, Captions, FallBack,
            Repeat, RotateTime, Shuffle, Smoothing, Volume, CallBack, EnableJS,
            JavascriptID, Link, LinkFromDisplay, LinkTarget, Recommendation, SearchLink,
            StreamScript, Type);
        }

        #endregion

    }
}
